#include <stdio.h>
#define Min(x,y) ((x) > (y) ? (y) : (x))

size_t MyStrlen(const char *s)
{
    size_t count = 0;
    while(*s != '\0')
    {
        s++;
        count++;
    }
    return count;
}

int MyStrcmp(const char *s1, const char *s2)
{
    while(*s1 == *s2 && *s1 != '\0' && *s2 != '\0')
    {
        s1++;
        s2++;
    }
    return *s1 - *s2;
}

int MyStrncmp(const char *s1, const char *s2, size_t n)
{
    n = Min(Min(MyStrlen(s1), MyStrlen(s2)), n);
    while(*s1 == *s2 && n != 1)
    {
        s1++;
        s2++;
        n--;
    }
    return *s1 - *s2;
}

void MyStrcpy(char *dest, const char *src)
{
    while(*src != '\0')
    {
        *dest++ = *src++;
    }
    *dest = '\0';
}

void MyStrncpy(char *dest, const char *src, int n)
{
    while(n-- != 0)
    {
        *dest++ = *src++;
    }
    *dest = '\0';
}

void MyStrcat(char *dest, const char* src)
{
    size_t dest_len = MyStrlen(dest);
    MyStrcpy(dest + MyStrlen(dest), src);
}

void MyStrncat(char *dest, const char* src, int n)
{
    MyStrncpy(dest + MyStrlen(dest), src, n);
}

//BF算法（暴力匹配算法）
//KMP算法
char *MyStrstr(char *dest, const char *src)
{
    size_t dlen = MyStrlen(dest);
    size_t slen = MyStrlen(src);

    if(dlen < slen)
    {
        return 0;
    }

    for(int i = 0; i <= dlen - slen; i++)
    {
        if(MyStrncmp(dest + i, src, slen) == 0)
        {
            return dest + i;
        }
    }
    return NULL; 
}

char* MyStrtok(char *src, char *delim)
{
    //上一次切割后的下一个字符串首地址
    static char *nextStr = NULL;
    //想要切割上一个的字符串
    if(src == NULL)
    {
        src = nextStr;
    }
    if(src == NULL)
    {
        return NULL;
    }

    //跳过所有的分隔符
    while(*src != '\0')
    {
        char temp[] = {*src,'\0'};
        //当前字母就是分隔符
        if(MyStrstr(delim,temp) != NULL)
        {
            src++;
        }
        else
        {
            break;
        }
    }

    if(*src == '\0')
    {
        return NULL;
    }

    //src 有效字符串的首地址
    char *end = src;
    while(*end != '\0')
    {
        char temp[] = {*end, '\0'};
        //当前字符是分隔符
        if(MyStrstr(delim, temp) != NULL)
        {
            break;
        }
        //有效字符
        else
        {
            end++;
        }
    }
    //到字符串结尾
    if(*end == '\0')
    {
        //下一个要切割的字符串就不存在了
        nextStr = NULL;
    }
    else
    {
        *end = '\0';
        //下一个字符串的地址
        nextStr = end + 1;
    }

    return src;   
}

int main()
{
#if 1
    printf("%ld\n", MyStrlen("hello"));
#endif

#if 0
    char s1[] = "hello";
    char s2[] = "iello";

    printf("%d\n", MyStrcmp(s1,s2));
#endif

#if 0
    char s1[] = "hell";
    char s2[10];
    MyStrcpy(s2,s1);

    printf("%s\n",s2);
#endif

#if 0
    char s1[20] = "hello";
    char s2[] = "world";
    MyStrcat(s1,s2);

    printf("%s\n", s1);
#endif

#if 0
    char s1[20] = "e";
    char s2[] = "eoa";

    printf("%d\n", MyStrstr(s1, s2));
#endif

#if 0
    char s[] = ",,,jack,july!jue.yes";
    char *dest = MyStrtok(s, ",!.");
    while(dest != NULL)
    {
        printf("%s\n", dest);
        dest = MyStrtok(NULL, ",!.");
    }
#endif
      
    return 0;
}